package com.game.view.display
{
    import com.game.view.IAnimation;
    import com.game.view.ViewerBase;
    
    /**
     * 帧序列为1-总帧数的MovieClip实现
     */
    
    public class AnimationClip extends ViewerBase implements IAnimation
    {
        
        protected var _totalFrames:int = 1;
        
        protected var _loopType:int = AnimationLoopType.RESET_WHEN_END;
        
        protected var _playing:Boolean = true;
        
        protected var _prevFrame:int = 0;
        
        protected var _currentFrame:int = 1;
        
        protected var _loopTime:int = 0;
        
        protected var _isCompleted:Boolean = false;
        
        public function AnimationClip()
        {
            super();
            needDestroyWhenRemove = false;
            _totalFrames = 1;
            _currentFrame = 1;
        }
        
        override protected function beforeAdded():void
        {
            buttonMode = false;
            mouseEnabled = false;
            mouseChildren = false;
        }
        
        public function renderAnimate():void
        {
            if (!_playing) return;
            NextFrame();
        }
        
        protected function render():void
        {
        }
        
        public function GotoAndPlay(frame:int):void
        {
            if (frame > _totalFrames) frame = _totalFrames;
            if (frame <= 0) frame = 1;
            _currentFrame = frame;
            _playing = true;
            render();
        }
        
        public function GotoAndStop(frame:int):void
        {
            if (frame > _totalFrames) frame = _totalFrames;
            if (frame <= 0) frame = 1;
            _currentFrame = frame;
            _playing = false;
            render();
        }
        
        public function Play():void
        {
            _playing = true;
        }
        
        public function Stop():void
        {
            _playing = false;
        }
        
        public function Pause():void
        {
            _playing = false;
        }
        
        public function NextFrame():void
        {
            if (_currentFrame < _totalFrames)
            {
                _currentFrame++;
                render();
                return;
            }
            if (_currentFrame >= _totalFrames)
            {
                switch (_loopType)
                {
                    case AnimationLoopType.STOP_WHEN_END:
                        _currentFrame = _totalFrames;
                        _isCompleted = true;
                        _playing = false;
                        break;
                    case AnimationLoopType.RESET_WHEN_END:
                        _currentFrame = 1;
                        render();
                        _loopTime++;
                        break;
                    case AnimationLoopType.REMOVE_WHEN_END:
                        removeFromParent();
                        break;
                    default:
                        throw "Unknown Loop Type";
                }
            }
        }
        
        public function PrevFrame():void
        {
            if (_currentFrame > 1) _currentFrame--;
            render();
        }
        
        public function Destroy():void
        {
        
        }
        
        public function get loopType():int
        {
            return _loopType;
        }
        
        public function set loopType(v:int):void
        {
            _loopType = v;
        }
        
        override public function get isPlaying():Boolean
        {
            return _playing;
        }
        
        public function get totalFrame():int
        {
            return _totalFrames;
        }
        
        public function get frameLeft():int
        {
            return _totalFrames - _currentFrame;
        }
        
        override public function get currentFrame():int
        {
            return _currentFrame;
        }
        
        public function get isComplete():Boolean
        {
            return _isCompleted;
        }
        
        public function get loopTime():int
        {
            return _loopTime;
        }
    }
}
